<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>导入入库单</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="/static/layui/css/layui.css" />
<link rel="stylesheet" href="/static/ui/css/ui.css" />
	</head>
	<body>
		<form class="layui-form" action="" lay-filter="editform">
			<div class="mainBox">
				<div class="main-container">
					<div class="main-container">
						<div class="layui-form-item">
							<label class="layui-form-label">下载模版</label>
							<div class="layui-input-block" id="cate_tpl">
								<!--a href="/static/tpl/stock/repair.xlsx" class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm" 
					target="_blank">维修板件</a>
					<a href="/static/tpl/stock/repair2.xlsx" class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm" 
					target="_blank">维修元器件</a>
					<a href="/static/tpl/stock/left.xlsx" class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm"
					 target="_blank">剩余物料</a>
					<a href="/static/tpl/stock/oa.xlsx" class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm"
					 target="_blank">办公设备</a-->
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">说明</label>
							<div class="layui-input-block">
								物料名称如果系统没有将按分类自动创建。如使用了字典数据请确保字典数据存在。
							</div>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label">xlsx文件</label>

							<div class="layui-input-block">
								<button type="button" class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm"
									id="uploadPath">
									<i class="layui-icon">&#xe67c;</i>上传
								</button>
								<input type="hidden" name="path" value="" id="path" placeholder="*格式" readonly>

							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">仓库地址</label>
							<div class="layui-input-block">
								<button class="layui-btn" id="chooseArea">
									<span id="areaText">选择仓库</span>
									<i class="layui-icon layui-icon-down layui-font-12"></i>
								</button>
								<span id="barText"></span>
							</div>
						</div>

						<!--div class="layui-form-item layui-form-text">
				<label class="layui-form-label">备注</label>
				<div class="layui-input-block">
				<textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
				</div>
			</div-->

					</div>
				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<input type="hidden" name="area_id" id="area_id" value="0" />
					<input type="hidden" name="bar_id" id="bar_id" value="0" />
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="stock_in-save">
						<i class="layui-icon layui-icon-ok"></i>
						导入
					</button>
				</div>
			</div>
		</form>
		<script src="../config.js"></script>
		<script src="/static/layui/layui.js"></script>
		<script src="/static/ui/ui.js"></script>
		<script>
			layui.use(['form', 'layer', 'tree', 'laydate', 'upload', 'dropdown','FileSaver'], function() {
				let form = layui.form,
					layer = layui.layer,
					$ = layui.$,
					req = _req(),
					dropdown = layui.dropdown,
					//FileSaver = layui.FileSaver,
					upload = layui.upload,
					laydate = layui.laydate,
					goods_id = req.goods_id;
				form.verify({
					len: (val, item) => {
						let min = item.getAttribute("min")
						if (val.length < min) {
							return '长度不能小于' + min;
						}
					}
				})
				upload.render({
					elem: '#uploadPath' //绑定元素
						,
					url: apiUrl + 'upload/index' //上传接口
						,
					headers: getHeader(),
					exts: 'xlsx',
					field: 'file'
						//,acceptMime: 'image/*'
						,
					accept: 'file',
					done: function(res) {
						//上传完毕回调
						console.log(res)
						$('#path').val(res.data.path)
						layer.msg('上传成功')
					},
					error: function() {
						//请求异常回调

					}
				});
				
				_get(layui, 'stock_in/importBefore', res => {
					res.cates.forEach(el => {
						let tpl = `<span src="${el.id}" class="layui-btn cate_tpl layui-btn-primary layui-border-blue layui-btn-sm">${el.name}</span>`;
						$("#cate_tpl").append(tpl);	
					})

					$("#cate_tpl").click(e => {
						//console.log()
						let cid = $(e.target).attr('src') * 1;
						if (cid)
							_get(layui, 'stock_in/downXlsxTpl?cid=' + cid, d => {
								//console.log(d)
								let link = document.createElement('a');
								link.style.display = 'none';
								link.href = d.url;
								link.setAttribute('download',d.filename);
								document.body.appendChild(link);
								link.click();
								document.body.removeChild(link);
							})
					})
					// form.render("select");
					dropdown.render({
						elem: '#chooseArea',
						data: res.area,
						trigger: 'hover',
						click: (d, el) => {
							//console.log(d)
							$('#areaText').text(d.topname);
							$('#barText').text(d.title);
							$('#area_id').val(d.pid);
							$('#bar_id').val(d.id);
						}
					})
				});

				//监听提交
				form.on('submit(stock_in-save)', function(data) {
					data = data.field;
					let postUrl = 'stock_in/import';
					// if(req.id) {
					//     postUrl = 'stock_in/edit';
					// }else{
					//     postUrl = 'stock_in/add';
					// }
					_post(layui, postUrl, data, res => {
						//console.log(res)
						parent.layui.table.reload("stock_in-table");
						parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页

					})

					return false;
				});

			});
		</script>

	</body>
</html>
